package com.laobindj.siji.utils;

import android.content.Context;
import android.content.SharedPreferences;


import java.util.Map;

public class SpUtilsNew {
    private static SharedPreferences mSp;

    public static void init(Context context){

        mSp=context.getSharedPreferences("TOPSPROSYS", Context.MODE_PRIVATE);

    }
    /**
     * 保存
     *
     * @param key
     * @param value
     */
    public static void save(String key, Object value) {
        SharedPreferences.Editor editor = mSp.edit();
        if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        } else {
            editor.putString(key, value.toString());
        }

        editor.apply();
    }
 public static  void putString(String key, Object value){
     SharedPreferences.Editor editor = mSp.edit();
        editor.putString(key, (String) value);
        editor.apply();
 }
 public static String getString(String key, String defaultValue){

     return mSp.getString(key,defaultValue);
 }
    /**
     * 取值
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static Object get(String key, Object defaultValue) {
        if (defaultValue instanceof String) {
            return mSp.getString(key, (String) defaultValue);
        } else if (defaultValue instanceof Integer) {
            return mSp.getInt(key, (Integer) defaultValue);
        } else if (defaultValue instanceof Boolean) {
            return mSp.getBoolean(key, (Boolean) defaultValue);
        } else if (defaultValue instanceof Float) {
            return mSp.getFloat(key, (Float) defaultValue);
        } else if (defaultValue instanceof Long) {
            return mSp.getLong(key, (Long) defaultValue);
        } else {
            return null;
        }
    }

    public static boolean get(String key, Boolean defaultValue) {
        return mSp.getBoolean(key, (Boolean) defaultValue);
    }
    public static String get(String key, String defaultValue) {
        Map map=mSp.getAll();
        return mSp.getString(key, (String) defaultValue);
    }
    public static void  clearAll(){
        SharedPreferences.Editor editor = mSp.edit();
        editor.clear();
        editor.apply();;
    }
    /**
     * 删除
     *
     * @param key
     */
    public static void remove(String key) {
        SharedPreferences.Editor editor = mSp.edit();
        editor.remove(key);

        editor.apply();;
    }


    public static boolean contains(String key){
       String value= get(key,"");
       if("".equals(value)){
           return false;
       }
       return true;
    }




}
